package com.wyj.server;

import com.wyj.lib.HelloReply;
import com.wyj.lib.HelloRequest;
import com.wyj.lib.MyServiceGrpc;
import io.grpc.stub.StreamObserver;
import net.devh.boot.grpc.server.service.GrpcService;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author ：wyj
 * @description: 书写我们服务端业务逻辑
 * @date ：2022/12/25 9:49 下午
 */
@GrpcService
public class MyServiceImpl extends MyServiceGrpc.MyServiceImplBase {

    /**
     * @description
     * syntax = "proto3";
     *
     * option java_generic_services = true;
     * option java_multiple_files = true;
     * option java_package = "com.wyj.lib";
     * option java_outer_classname = "HelloWorldProto";
     *
     * // The greeting service definition.
     * service MyService {
     *     // Sends a greeting
     *     rpc SayHello (HelloRequest) returns (HelloReply) {
     *     }
     * }
     *
     * // The request message containing the user's name.
     * message HelloRequest {
     *     string name = 1;
     * }
     * // The response message containing the greetings
     * message HelloReply {
     *     string message = 1;
     * }
     *
     * HelloRequest request: request 客户端请求参数。
     *
     */
    @Override
    public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
        String name = request.getName();
        long time = Long.parseLong(name);
        // 根据时间戳查询相应的时间
        Date date = new Date(time);
        // 格式化时间
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = formatter.format(date);
        HelloReply reply = HelloReply.newBuilder()
                .setMessage(formattedDateTime)
                .build();

        responseObserver.onNext(reply);
        responseObserver.onCompleted();
    }
}
